package site.sorghum.ddl.solon.config;

import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import site.sorghum.ddl.mybatis.flex.FlexDdlWrapper;
import site.sorghum.ddl.mybatis.flex.FlexEntityGenerate;
import site.sorghum.ddl.mybatis.flex.FlexInitConfig;
import site.sorghum.ddl.solon.prop.GlobalConfigProp;

import javax.sql.DataSource;

@Configuration
@org.springframework.context.annotation.Configuration
@Condition(onClass = FlexInitConfig.class)
@ConditionalOnClass(value = FlexInitConfig.class)
public class FlexConfig {

    @Bean
    @org.springframework.context.annotation.Bean
    FlexEntityGenerate flexEntityGenerate() {
        return new FlexEntityGenerate();
    }


    @Bean
    @org.springframework.context.annotation.Bean
    FlexDdlWrapper flexDdlWrapper(@Autowired @Inject DataSource dataSource) {
        return new FlexDdlWrapper(dataSource);
    }


    @Bean
    @org.springframework.context.annotation.Bean
    FlexInitConfig flexInitConfig(
            @Autowired(required = false) @Inject(required = false) GlobalConfigProp globalConfig,
            @Autowired @Inject FlexDdlWrapper flexDdlWrapper,
            @Autowired @Inject FlexEntityGenerate flexEntityGenerate) {
        if (globalConfig != null) {
            globalConfig.init();
        }
        return new FlexInitConfig(flexDdlWrapper, flexEntityGenerate);
    }
}
